﻿using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.AsmType;
using System;

namespace AgencyManagerSystem.Core
{
    public class SupplierMemberUpdaterCreator : BaseUpdaterCreator<SupplierMemberModel>
    {
        SupplierMemberFetcher supplierMemberFetcher = new SupplierMemberFetcher();
        public SupplierMemberUpdater CreateSupplierUpdaterInstance()
        {
            SupplierMemberUpdater supplierMemberUpdater = new SupplierMemberUpdater(supplierMemberFetcher);
            supplierMemberUpdater.DataUpdating += FlagAndApproveFlagValid;
            supplierMemberUpdater.DataUpdating += NotSpaceCheck;
            supplierMemberUpdater.DataUpdating += ReferExistCheck;
            supplierMemberUpdater.DataUpdating += CodeUniqueCheck;
            supplierMemberUpdater.DataUpdating += UserUniqueCheck;
            return supplierMemberUpdater;
        }

        private void UserUniqueCheck(BaseUpdater<SupplierMemberModel> sender, DataUpdatingEventArgs<SupplierMemberModel> e)
        {
            if (e.Insert == null)
                return;
            var model = supplierMemberFetcher.GetByUser(e.Insert.User);
            if (model != null && model.AutoId != e.Insert.AutoId)
            {
                e.Cancel = true;
                e.Message = String.Format("成员{0}已存在于供应商{1}", model.Code, model.Supplier);
                return;
            }
        }

        private void CodeUniqueCheck(BaseUpdater<SupplierMemberModel> sender, DataUpdatingEventArgs<SupplierMemberModel> e)
        {
            if (e.Insert == null)
                return;
            var supplierMemberFetcher = new SupplierMemberFetcher();
            var model = supplierMemberFetcher.GetByCode(e.Insert.Supplier, e.Insert.Code);
            if(model!=null&& model.AutoId!=e.Insert.AutoId)
            {
                e.Cancel = true;
                e.Message = String.Format("编号{0}已存在于供应商成员{1}",model.Code,model.AutoId);
                return;
            }
        }

        private void ReferExistCheck(BaseUpdater<SupplierMemberModel> sender, DataUpdatingEventArgs<SupplierMemberModel> e)
        {
            if (e.Insert == null)
                return;
            var userFetcher = new UserFetcher();
            var supplierFetcher = new SupplierFetcher();
            if(userFetcher.Get(e.Insert.Supplier)==null)
            {
                e.Cancel = true;
                e.Message = "对应供应商不存在";
                return;
            }
            if(userFetcher.Get(e.Insert.User)==null)
            {
                e.Cancel = true;
                e.Message = "对应用户不存在";
                return;
            }
        }

        private void NotSpaceCheck(BaseUpdater<SupplierMemberModel> sender, DataUpdatingEventArgs<SupplierMemberModel> e)
        {
            if (e.Insert == null)
                return;
            if(String.IsNullOrWhiteSpace(e.Insert.Code)||String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Cancel = true;
                e.Message = "代号或名称不能为空";
                return;
            }
        }
    }
}
